<template>
  <transition-group
    tag="ul"
    :class="[
      'el-upload-list',
      'el-upload-list--' + listType,
      { 'is-disabled': disabled }
    ]"
    name="el-list"
  >
    <li
      v-for="(file, index) in files"
      :class="['el-upload-list__item', 'is-' + file.status, focusing ? 'focusing' : '']"
      :key="index"
      tabindex="0"
      @keydown.delete="!disabled && handleRemove(index)"
      @focus="focusing = true"
      @blur="focusing = false"
      @click="focusing = false"
    >
      <img
        class="el-upload-list__item-thumbnail"
        v-if="['picture-card', 'picture'].indexOf(listType) > -1"
        :src="file.url" alt=""
      >
      <a class="el-upload-list__item-name" @click="handleClick(file)">
        <i class="el-icon-document"></i>{{file.name}}
      </a>
      <label class="el-upload-list__item-status-label">
        <i :class="{
          'el-icon-upload-success': true,
          'el-icon-circle-check': listType === 'text',
          'el-icon-check': ['picture-card', 'picture'].indexOf(listType) > -1
        }"></i>
      </label>
      <i class="el-icon-close" v-if="!disabled" @click="handleRemove(index)"></i>
      <i class="el-icon-close-tip" v-if="!disabled">删除</i>
      <el-progress
        v-if="file.status === 'uploading'"
        :type="listType === 'picture-card' ? 'circle' : 'line'"
        :stroke-width="listType === 'picture-card' ? 6 : 2"
        :percentage="parsePercentage(file.percentage)">
      </el-progress>
      <span class="el-upload-list__item-actions" v-if="listType === 'picture-card'">
        <span
          class="el-upload-list__item-preview"
          v-if="handlePreview && listType === 'picture-card'"
          @click="handlePreview(file)"
        >
          <i class="el-icon-zoom-in"></i>
        </span>
        <span
          v-if="!disabled"
          class="el-upload-list__item-delete"
          @click="handleRemove(index)"
        >
          <i class="el-icon-delete"></i>
        </span>
      </span>
      <el-dialog :visible.sync="dialogVisible" title="预览" append-to-body>
        <img width="100%" :src="dialogImageUrl" alt="">
      </el-dialog>
    </li>
  </transition-group>
</template>
<script>
  export default {
    name: 'HPictureList',
    data() {
      return {
        focusing: false,
        dialogVisible: false,
        dialogImageUrl: '',
      }
    },
    props: {
      files: {
        type: Array,
        default() {
          return []
        }
      },
      disabled: {
        type: Boolean,
        default: false
      },
      handlePreview: {
        type: Function,
        default: function (file) {
          this.dialogImageUrl = file.url
          this.dialogVisible = true
        }
      },
      listType: {
        type: String,
        default: 'picture-card'
      }
    },
    methods: {
      parsePercentage(val) {
        return parseInt(val, 10)
      },
      handleClick(file) {
        this.handlePreview && this.handlePreview(file)
      },
      handleRemove: function (index) {
        this.files.splice(index, 1)
      }
    }
  }
</script>
